package com.sky.controller.admin;


import com.atom.module.common.result.PageResult;
import com.atom.module.common.result.Result;
import com.sky.common.constant.JwtClaimsConstant;
import com.sky.common.properties.JwtProperties;
import com.sky.common.utils.JwtUtil;
import com.sky.pojo.dto.EmployeeDTO;
import com.sky.pojo.dto.EmployeeLoginDTO;
import com.sky.pojo.dto.EmployeePageQueryDTO;
import com.sky.pojo.entity.Employee;
import com.sky.pojo.vo.EmployeeLoginVO;
import com.sky.service.EmployeeService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "员工相关接口")
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @Operation(summary = "员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{} , {}", employeeLoginDTO, jwtProperties.toString());

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());

        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder().id(employee.getId()).userName(employee.getUsername()).name(employee.getName()).token(token).build();

        return Result.success(employeeLoginVO);

//        String uri = "http://localhost:8080/admin/employee/login";
//
//        // 构建 HttpEntity
//        HttpEntity<EmployeeLoginDTO> request = new HttpEntity<>(employeeLoginDTO);
//
//        ResponseEntity<Result<EmployeeLoginVO>> exchange = restTemplate.exchange(
//                uri,
//                HttpMethod.POST,
//                request,
//                new ParameterizedTypeReference<Result<EmployeeLoginVO>>() {
//                });
//
//        log.info("员工登录 result：{}", exchange);
//        return exchange.getBody();
    }

    /**
     * 员工登出
     *
     * @return Result<Boolean>
     */
    @Operation(summary = "员工登出")
    @PostMapping("/logout")
    public Result<?> logout() {
        // 获取此时请求头重的 token,并设置其过期即可
        return Result.success();
    }

    /**
     * 新增员工
     *
     * @param employeeDTO
     * @return
     */
    @Operation(summary = "新增员工")
    @PostMapping()
    public Result<Long> add(@RequestBody EmployeeDTO employeeDTO) {
        log.info("EmployeeController:线程id={},新增员工：{}", Thread.currentThread().getId(), employeeDTO);
        Long insertId = employeeService.addEmp(employeeDTO);
        return Result.success(insertId);
    }

    /**
     * 删除员工
     */
    @Operation(summary = "删除员工")
    @DeleteMapping()
    public Result<Boolean> delete(@RequestParam("id") Long empId) {
        log.info("EmployeeController:线程id={},删除员工：{}", Thread.currentThread().getId(), empId);
        return Result.success(employeeService.deleteEmp(empId));
    }

    /**
     * 查询员工,分页
     */
    @Operation(summary = "员工分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO dto) {
        log.info("员工分页查询：{}", dto);
        PageResult pageResult = employeeService.page(dto);
        return Result.success(pageResult);
    }

    @Operation(summary = "启用/禁用员工")
    @PostMapping("/status/{status}")
    public Result<?> active(@PathVariable Integer status, Long id) {
        log.info("启用禁用员工：status={}, id={}", status, id);
        employeeService.enableOrDisable(status, id);
        return Result.success();
    }

    @Operation(summary = "查询员工,ID")
    @GetMapping("/{id}")
    public Result<Employee> get(@PathVariable Long id) {
        log.info("回显员工：id={}", id);
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    /**
     * 编辑更新员工
     *
     * @param dto
     * @return
     */
    @Operation(summary = "编辑更新员工")
    @PutMapping
    public Result update(@RequestBody EmployeeDTO dto) {
        log.info("编辑更新员工：{}", dto);
        employeeService.update(dto);
        return Result.success();
    }

}
